/**
 * @Project: Lens
 * @Title: PicZoomActivity.java
 * @Package: com.hh.lens.ui
 * @Description:
 * @Author: hh
 * @Date: 2013-2-6 上午1:11:51
 * @Copyright: 2013 HH Inc. All right reserved
 * @Version:
 */
package com.xexun.gpstracker.ui;

import java.io.InputStream;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.ImageView;

import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.base.BaseActivity;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.listener.ImageTouchScaleListener;
import com.xexun.gpstracker.util.CommUtil;
import com.xexun.gpstracker.util.FileUtils;

/**
 * @Classname: PicZoomActivity
 * @Description:
 * @Author: hh
 * @Date: 2013-2-6 上午1:11:51
 * @Version:
 */
public class ImageZoomActivity extends BaseActivity {
    private ImageView imageView;

    /*
     * (non-Javadoc)
     *
     * @Title: onCreate
     *
     * @Description:
     *
     * @param savedInstanceState
     *
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zoom_imageview);
        init();
    }

    /**
     * @Methodname: init
     * @Discription:
     * @Author HH
     * @Time 2013-2-6 上午1:20:58
     */
    private void init() {
        String imgPath = getIntent().getStringExtra(Constants.PIC_PATH);
        if (CommUtil.isNotBlank(imgPath)) {
            imageView = (ImageView) findViewById(R.id.imageZoom);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(imgPath, options);
            if (options.outHeight < 0 || options.outWidth < 0) {
                new AlertDialog.Builder(this).setTitle(R.string.msg_notify).setMessage(R.string.cannot_found_pic)
                        .setPositiveButton(R.string.confirm, new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).show();
            } else {
                InputStream in = null;
                try {
                    in = getContentResolver().openInputStream(Uri.parse(Constants.SCHEME_FILE + imgPath));
                    options.inJustDecodeBounds = false;
                    Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
                    imageView.setImageBitmap(bitmap);
                } catch (Exception e) {
                    Log.e(ImageZoomActivity.class.getName(), "加载图片失败！", e);
                    CommUtil.showMsgShort(getString(R.string.load_pic_failed));
                } finally {
                    FileUtils.closeInputStream(in);
                }

                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);// 获取屏幕分辨率
                ImageTouchScaleListener listener = new ImageTouchScaleListener(imageView, dm, options.outWidth, options.outHeight);
                imageView.setOnTouchListener(listener);

//                ImageLoader.getInstance().displayImage(Constants.SCHEME_FILE + imgPath, imageView, new SimpleImageLoadingListener() {
//                    @Override
//                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//                        DisplayMetrics dm = new DisplayMetrics();
//                        getWindowManager().getDefaultDisplay().getMetrics(dm);// 获取屏幕分辨率
//                        ImageTouchScaleListener listener = new ImageTouchScaleListener(imageView, dm, loadedImage.getScaledWidth(dm), loadedImage.getScaledHeight(dm));
//                        imageView.setOnTouchListener(listener);
//                    }
//                });
            }
        }
    }
}
